
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>03_M文件 - fubaisen</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="三、M文件1、脚本文件与函数文件Matlab的M文件可分为脚本文件(Matlab scripts)和函数文件(Matlab functions)

脚本文件是包含多条Matlab命令的文件

函数文,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="fubaisen" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="03_M文件 - fubaisen"/>
    <meta name="twitter:description" content="三、M文件1、脚本文件与函数文件Matlab的M文件可分为脚本文件(Matlab scripts)和函数文件(Matlab functions)

脚本文件是包含多条Matlab命令的文件

函数文,"/>
    
    
    
    
    <meta property="og:site_name" content="fubaisen"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="03_M文件 - fubaisen"/>
    <meta property="og:description" content="三、M文件1、脚本文件与函数文件Matlab的M文件可分为脚本文件(Matlab scripts)和函数文件(Matlab functions)

脚本文件是包含多条Matlab命令的文件

函数文,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">fubaisen</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://fubaisen.github.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">03_M文件</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">03_M文件</h1>
        <div class="stuff">
            <span>四月 03, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/MATLAB%E5%AD%A6%E4%B9%A0/" rel="tag">MATLAB学习</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="三、M文件"><a href="#三、M文件" class="headerlink" title="三、M文件"></a>三、M文件</h1><h2 id="1、脚本文件与函数文件"><a href="#1、脚本文件与函数文件" class="headerlink" title="1、脚本文件与函数文件"></a>1、脚本文件与函数文件</h2><p>Matlab的M文件可分为脚本文件(Matlab scripts)和函数文件(Matlab functions)</p>
<ul>
<li><p>脚本文件是包含多条Matlab命令的文件</p>
</li>
<li><p>函数文件可以包含输入变量并把结果传给输出变量</p>
</li>
</ul>
<p>简要区别：</p>
<ul>
<li><p>1、脚本文件</p>
<ul>
<li><p>（1）多条命令的综合体</p>
</li>
<li><p>（2）没有输入、输出变量被调用</p>
</li>
<li><p>（3）所有变量均使用Matlab基本工作空间。</p>
</li>
<li><p>（2）没有函数声明行</p>
</li>
</ul>
</li>
<li><p>2、函数文件</p>
<ul>
<li><p>（1）常用于扩充Matlab函数库</p>
</li>
<li><p>（2）可以包含输入、输出变量，用于多次调用</p>
</li>
<li><p>（3）运算中生成的所有变量都存放在函数工作空间</p>
</li>
<li><p>（4）包含函数声明行，function 输出变量&#x3D;函数名称(输入变量)</p>
</li>
</ul>
</li>
</ul>
<table>
<thead>
<tr>
<th>脚本文件</th>
<th>函数文件</th>
</tr>
</thead>
<tbody><tr>
<td>脚本文件可以理解为简单的M文件 ，脚本文件中的变量都是全局变量</td>
<td><code>函数文件</code>中的变量都是<code>局部变量</code>，除非使用特别声明，函数运算完毕之后，其定义的变量将从工作区中清除</td>
</tr>
<tr>
<td>脚本文件只是将一系列相关的代码集合封装，没有输入参数和输出参数，既不自带参数也一定要返回结果</td>
<td>函数文件是在脚本文件的基础之上我<code>添加了一行函数声明行</code>，其代码，组织<code>结构和调用方式</code>与对应的脚本文件截然<code>不同</code></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td><code>函数文件是以函数声明行作为开始</code>的，<code>相对于</code>用户在Matlab函数库里编写的<code>子函数</code></td>
</tr>
<tr>
<td></td>
<td>多数函数文件一般都是有输入和输出变量</td>
</tr>
</tbody></table>
<h2 id="2、函数文件的使用"><a href="#2、函数文件的使用" class="headerlink" title="2、函数文件的使用"></a>2、函数文件的使用</h2><p>函数文件的功能是建立一个函数，且这个文件与Matlab的库函数一样使用。[其扩展名为 <code>.m</code>](<a target="_blank" rel="noopener" href="https://zhidao.baidu.com/question/207454039.html">MATLAB默认扩展名是什么呀？matlab语言构成程序的文件是什么文件？_百度知道 (baidu.com)</a>) 。不能直接输入函数文件名来运行一个函数文件，它必须由其他语句来调用。函数文件允许有多个输入、输出参数值</p>
<h4 id="（1）函数定义"><a href="#（1）函数定义" class="headerlink" title="（1）函数定义"></a>（1）函数定义</h4><ul>
<li><p>语法格式：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span><span class="params">[f1,f2,f3,...]</span>=<span class="title">fun</span><span class="params">(x,y,z,...)</span>       </span></span><br><span class="line"><span class="comment">% f1,f2,f3,...表示形式输出参数   ;    </span></span><br><span class="line"><span class="comment">% x1,x2,x3,...表示形式输出参数   ;   </span></span><br><span class="line"><span class="comment">% fun 表示函数名</span></span><br></pre></td></tr></table></figure>

<p>调用函数格式：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[y1,y2,y3,...]=fun(x1,x2,x3,...)</span><br><span class="line"><span class="comment">% y1,y2,y3,...表示出参数</span></span><br><span class="line"><span class="comment">% x1,x2,x3,...表示输入参数</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="（2）函数说明"><a href="#（2）函数说明" class="headerlink" title="（2）函数说明"></a>（2）函数说明</h4><ul>
<li><p>如果在函数文件中插入了<code>return语句</code>，则当执行该语句时就结束函数的执行，程序流程  转至  <code>调用该函数的位置</code>。如果函数文件中<code>不含return</code>语句，则当被调用函数执行<code>完成后就自动返回</code>。</p>
</li>
<li><p>函数文件从形式上与脚本文件不同，函数文件的<code>第一行必须由关键字function引导，</code>对于<code>function[返回变量]=函数名称(输入变量)</code>,输入和返回变量的实际个数分别由 <code>nargin</code> 和 <code>nargout</code> 保留变量给出，无论是否直接使用这两个变量，只要进入该函数 Matlab就将<code>自动生成</code>这两个变量。</p>
</li>
<li><p>M文件<code>调用</code>的<code>函数名和文件名必须相同,</code>函数调用时参数顺序应与定义一致。</p>
</li>
<li><p><code>函数文件运行</code>时,Matlab为它<code>开辟</code>一个<code>临时函数工作空间</code>，由<strong>函数执行的命令以及由这些命令所创建的中间变量，都隐含其中</strong>。当文件<code>执行完毕</code>，该临时<code>工作空间及其中的变量立刻被清除</code>。只能看到输入和输出的内容，函数运行后只保留最后结果，不保留中间结果。<strong>函数中的变量均为局部变量</strong>。</p>
</li>
<li><p>函数<code>可</code>以<code>嵌套调用</code>，即一个函数可以被其他函数调用，甚至<code>可</code>以被<code>自身调用</code>，此时成为递归调用。</p>
</li>
<li><p>例：利用函数文件，实现直角坐标（x,y）到极坐标(r,θ)转换；建立   <code>bs_07_01.m</code></p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="params">[r,theta]</span> = <span class="title">bs_07_01</span><span class="params">(x,y)</span></span></span><br><span class="line">r = <span class="built_in">sqrt</span>(x^<span class="number">2</span>+y^<span class="number">2</span>);</span><br><span class="line">theta = <span class="built_in">atan</span>(y/x);</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">储存为 bs_07_01.m 文件 ，然后在命令行窗口输入：[r,theta]=bs_07_01(<span class="number">3</span>,<span class="number">4</span>) </span><br></pre></td></tr></table></figure>

<p>例：编写递归调用函数，求n！</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">f</span> = <span class="title">bs_07_02</span><span class="params">(n)</span></span></span><br><span class="line"><span class="keyword">if</span> n&lt;=<span class="number">1</span></span><br><span class="line">    f=<span class="number">1</span>;</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    f=bs_07_02(n<span class="number">-1</span>)*n;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line">储存为 bs_07_02.m 文件 ，然后在命令行窗口输入：bs_07_02(<span class="number">5</span>)</span><br></pre></td></tr></table></figure></li>
</ul>
<h4 id="（3）主函数与子函数"><a href="#（3）主函数与子函数" class="headerlink" title="（3）主函数与子函数"></a>（3）主函数与子函数</h4><p>（1）一个M文件可以包含多个函数，第一个函数为主函数，其他为子函数</p>
<p>（2）主函数必须方在最前边，子函数次序可以随意改变</p>
<p>（3）子函数仅能备注函数或同一文件的其他子函数所调用</p>
<p>（4）子函数仅能在主函数中编辑</p>
<ul>
<li><p>例</p>
<p>程序命令：</p>
<figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">c</span> = <span class="title">bs_07_03</span><span class="params">(a,b)</span></span></span><br><span class="line">c = fun1(a,b) *fun2(a,b);</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">c</span> = <span class="title">fun1</span><span class="params">(a,b)</span></span></span><br><span class="line">c = a^<span class="number">2</span>+b^<span class="number">2</span>;</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">c</span> = <span class="title">fun2</span><span class="params">(a,b)</span></span></span><br><span class="line">c = a^<span class="number">2</span>-b^<span class="number">2</span>;</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure></li>
</ul>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
